package Window;

import dao.Impl.UserDaoImpl;
import entity.User;

import javax.swing.*;

public class Register extends JFrame {
    public Register(){
        init();
    }

    public void init(){
    //创建窗口对象
        JFrame jFrame =new JFrame();

        jFrame.setSize(400,500);

        jFrame.setTitle("这是注册页面");
        jFrame.setDefaultCloseOperation(3);
        jFrame.setLocationRelativeTo(null);
        jFrame.setLayout(null);//设置布局为空


        JLabel nameStr= new JLabel("账号：");
        //设置位置
        nameStr.setBounds(80,70,50,30);
        jFrame.add(nameStr);


        JLabel paswoedStr= new JLabel("密码：");
        paswoedStr.setBounds(80,110,50,30);
        jFrame.add(paswoedStr);

        JLabel rePaswoedStr= new JLabel("确认密码：");
        rePaswoedStr.setBounds(50,150,70,30);
        jFrame.add(rePaswoedStr);

        //文本框
        JTextField nameID=new JTextField();
        nameID.setBounds(120,70,150,30);
        jFrame.add(nameID);

        //密码文本框
        JPasswordField passwordStr=new JPasswordField();
         passwordStr.setBounds(120,110,150,30);
        jFrame.add(passwordStr);

        //确认密码文本框
        JPasswordField rePasswordStr=new JPasswordField();
         rePasswordStr.setBounds(120,150,150,30);
        jFrame.add(rePasswordStr);


        JButton jButton2=new JButton("注册登陆");
          jButton2.setBounds(100,210,160,30);
          jFrame.add(jButton2);
          jButton2.addActionListener(e -> {
              UserDaoImpl userDao=new UserDaoImpl();

              String str0=new String(nameID.getText());
              String str1=new String(passwordStr.getText());
              String str2=new String(rePasswordStr.getText());

              if(str1.equals(str2)){
                  //System.out.println(userDao.findbyId(new User(str0)).size());
                  if(userDao.findbyId(new User(str0)).size()>0){
                      JOptionPane.showMessageDialog(null,"用户名已被注册","错误",1);
                  }else{
                      if(userDao.add(new User(str0,str1))>0){
                          JOptionPane.showMessageDialog(null,"注册成功！","成功！",-1);
                          jFrame.setVisible(false);
                          new Login();
                      }


                  }

              }else {
                  JOptionPane.showMessageDialog(null,"密码输入不一致！","错误",1);
              }


          });

        jFrame.setVisible(true);//显示控件
       // JButton jButton2=new JButton();

    }
}
